import pandas as pd
import tensorflow as tf
import numpy as np
from numpy import genfromtxt


def cut_train(data_x, data_y, train_size, test_size):
    """
    对杜比给的训练集在进行分割，分割成训练集和测试集
    :param data_x: 原来的测试集的x
    :param data_y: 原来的测试集的y
    :param train_size: 训练集大小
    :param test_size:  测试集大小
    :return: 分割后产生的训练集x和y，测试集的x和y
    """

    # 1:以相同顺序打乱data_x和data_y
    data_x_y = np.column_stack((data_x, data_y))
    np.random.shuffle(data_x_y)
    data_x
    a = np.size(data_y, 0)
    index = np.random.randint(0, np.size(data_y, 0), train_size)
    train_x = []
    train_y = []
    test_x = []
    test_y = []

    return train_x, train_y, test_x, test_y


class DataLoader():
    def __init__(self):
        """读取训练集和测试集数据"""
        self.train_data, self.train_label = self.get_data("trainFeature1.csv")  # 训练集x和y
        self.test_data, self.test_label = self.get_data("testFeature1.csv")  # 测试集x和y
        self.num_train_data, self.num_test_data = self.train_data.shape[0], self.test_data.shape[0]  # 训练集和测试集的大小


    def get_batch(self, batch_size):
        """
        从训练集获取一个batch
        :param batch_size:
        :return:
        """
        # 从数据集中随机取出batch_size个元素并返回
        index = np.random.randint(0, self.num_train_data, batch_size)
        return self.train_data[index, :], self.train_label[index]

    def get_data(self,filepath1, filepath2="", filepath3=""):
        """
        从一个csv文件中读取数据到二维numpy
        :param filepath1:
        :param filepath2:
        :param filepath3:
        :return:
        """
        # train_x = []  # 训练集数据【515，30】
        # train_y = []  # 训练集标签【515,】
        data = genfromtxt(filepath1, delimiter=',', skip_header=1)
        X = data[:, 1:31]
        y = data[:, 31]
        return X, y




